#!/usr/bin/env python
# -*- coding: utf-8; -*-
from django import forms
from django.core.exceptions import ValidationError
from web.forms.bootstrap import BootstrapForm
from web import models

from utils.tencent.cos import check_file

class FolderModelForm(BootstrapForm, forms.ModelForm):
    class Meta:
        model = models.FileRepository
        fields = ['name']

    # 表单初始化时，绑定要显示或创建的文件的父目录对象
    def __init__(self, request, parent_object, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.request = request
        self.parent_object = parent_object

    def clean_name(self):
        name = self.cleaned_data['name']

        # 避免 *当前* 文件夹重名，因为所有的文件对象都位于一张表中，所以需
        # 要 self.parent_object
        queryset = models.FileRepository.objects.filter(
            file_type=2, name=name, project=self.request.tracer.project)
        # 查看同目录是否有同名文件
        if self.parent_object:
            exists = queryset.filter(parent=self.parent_object).exists()
        else:
            exists = queryset.filter(parent__isnull=True).exists()
        if exists:
            raise ValidationError('文件夹已存在')
        return name


class FileModelForm(forms.ModelForm):
    # Etag 是cos中上传文件md5值，ajax上传成功后，将该值传给后端，django 用
    # 这个 Etag 与 cos 桶中刚刚上传的文件 Etag 作对比，验证前端上传后发送到
    # 后端的信息的有效性，避免前端部分跳过cos上传步骤，直接向后台恶意向数据
    # 库发送虚假数据。保证数据库信息与 cos 的一致性

    etag = forms.CharField(label='ETag')

    class Meta:
        model = models.FileRepository
        exclude = ['project', 'file_type', 'update_user', 'update_datetime']

    def __init__(self, request, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.request = request

    def clean_file_path(self):
        return "https://{}".format(self.cleaned_data['file_path'])

    """
    def clean(self):
        key = self.cleaned_data['key']
        etag = self.cleaned_data['etag']
        size = self.cleaned_data['size']

        if not key or not etag:
            return self.cleaned_data

        # 向COS校验文件是否合法
        # SDK的功能
        from qcloud_cos.cos_exception import CosServiceError
        try:
            result = check_file(self.request.tracer.project.bucket, self.request.tracer.project.region, key)
        except CosServiceError as e:
            self.add_error("key", '文件不存在')
            return self.cleaned_data

        cos_etag = result.get('ETag')
        if etag != cos_etag:
            self.add_error('etag', 'ETag错误')

        cos_length = result.get('Content-Length')
        if int(cos_length) != size:
            self.add_error('size', '文件大小错误')

        return self.cleaned_data
    """
